package org.shuma.supplier.mng.impl;

import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuma.domain.pojo.Result;
import com.shuma.utils.HttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.shuma.supplier.domain.dto.SupplierDTO;
import org.shuma.supplier.domain.dto.UpdateSupplierDTO;
import org.shuma.supplier.domain.entity.Supplier;
import org.shuma.supplier.domain.vo.SupplierVO;
import org.shuma.supplier.facade.SupplierFacade;
import org.shuma.supplier.mapper.SupplierMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author :Wjh
 * @since :2024/6/17 上午11:50
 */

@Slf4j
@Service
@SofaService(interfaceType = SupplierFacade.class, uniqueId = "${service.unique.id}",
        bindings = {@SofaServiceBinding(bindingType = "bolt")})
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier>
        implements SupplierFacade {

    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public Result searchSupplier(String supplierName) {
        Result result = new Result();

        Supplier supplier = getOne(
                new LambdaQueryWrapper<Supplier>().like(Supplier::getSupplierName, supplierName)
        );

        SupplierVO supplierVO = new SupplierVO();

        try {
            BeanUtils.copyProperties(supplier, supplierVO);
        } catch (Exception exception) {
            log.info("copyProperties error");
            return result.error(HttpCodeEnum.PARAMS_ERROR, "");
        }

        log.info("supplierDTO:{}", supplierVO);

        return result.ok(supplierVO);
    }

    @Override
    public Result getSupplier(Integer id) {
        Result result = new Result();

        Supplier supplier = getById(id);

        SupplierVO supplierVO = new SupplierVO();

        try {
            BeanUtils.copyProperties(supplier, supplierVO);
        } catch (Exception exception) {
            log.info("copyProperties error");
            return result.error(HttpCodeEnum.PARAMS_ERROR, "");
        }

        log.info("supplierDTO:{}", supplierVO);

        return result.ok(supplierVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addSupplier(SupplierDTO supplierDTO) {
        Result result = new Result();

        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierDTO, supplier);

        Supplier one = supplierMapper.getSupplierByName(supplierDTO.getSupplierName());

        if (one != null && one.getIsDelete() == 1) {
            supplier.setSupplierId(one.getSupplierId());
            supplier.setIsDelete(0);
            supplier.setCreateTime(one.getCreateTime());
            supplier.setUpdateTime(LocalDateTime.now());
            supplierMapper.updateSupplierById(supplier);
        } else {
            try {
                save(supplier);
                log.info("保存成功");
            } catch (Exception exception) {
                log.info("保存失败");
                return result.error(HttpCodeEnum.SUPPLIER_NAME_EXIST, "");
            }
        }

        return result.ok(supplier.getSupplierId());
    }

    @Override
    public Result deleteSupplier(Integer supplierId) {
        Result result = new Result();

        log.info("supplierId:{}", supplierId);
        Supplier supplier = getById(supplierId);

        if (supplier == null) {
            return result.error(HttpCodeEnum.SUPPLIER_NOT_EXITS, "");
        }
        try {
            removeById(supplierId);
        } catch (Exception exception) {
            return result.error(HttpCodeEnum.PARAMS_ERROR, "");
        }
        return result.ok();
    }

    @Override
    public Result updateSupplier(UpdateSupplierDTO updateSupplierDTO) {
        Result result = new Result();
        Supplier supplier = getById(updateSupplierDTO.getSupplierId());

        if (supplier == null) {
            return result.error(HttpCodeEnum.SUPPLIER_NOT_EXITS, "");
        }
        supplier.setSupplierName(updateSupplierDTO.getSupplierName());
        supplier.setSupplierPhone(updateSupplierDTO.getSupplierPhone());
        supplier.setUpdateTime(LocalDateTime.now());
        try {
            updateById(supplier);
        } catch (Exception exception) {
            return result.error(HttpCodeEnum.PARAMS_ERROR, "");
        }
        return result.ok();
    }
}
